package com.aggron.concurrency.examples.atomic;

import com.aggron.concurrency.annotations.ThreadSafe;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author LR2SD
 * @description AtomicIntegerFieldUpdater原子并发测试类
 * @date 2018-06-10 09:15
 */
@Slf4j
@ThreadSafe
public class AtomicExample4 {

   private static AtomicIntegerFieldUpdater<AtomicExample4> updater=AtomicIntegerFieldUpdater.newUpdater(AtomicExample4.class,"count");


    /**
     * 必须时volatile修饰，非static修饰
     */
    @Getter
   private volatile int count=100;

   private static AtomicExample4 atomicExample4=new AtomicExample4();

   public static void main(String [] args){
       if(updater.compareAndSet(atomicExample4,100,120)){
           log.info("update success 1 {}",atomicExample4.getCount());
       }
       if(updater.compareAndSet(atomicExample4,100,120)){
           log.info("update success 2 {}",atomicExample4.getCount());
       }else {
           log.info("update failed  {}",atomicExample4.getCount());
       }
   }

}
